package com.example.clock;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class AddPointDataDialog extends Dialog {

    private EditText department;
    private EditText workTime;
    private EditText pointTime;
    private Button choseData;
    private Button cancel;
    private Button ok;
    private Context context;
    private OnButtonClickListener listener;

    public void setListener(OnButtonClickListener listener) {
        this.listener = listener;
    }

    public AddPointDataDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public AddPointDataDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AddPointDataDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.add_point_dialog);
        department = (EditText) findViewById(R.id.department);
        workTime = (EditText) findViewById(R.id.workTime);
        pointTime = (EditText) findViewById(R.id.pointTime);
        choseData = (Button) findViewById(R.id.choseData);
        cancel = (Button) findViewById(R.id.cancel);
        ok = (Button) findViewById(R.id.ok);
        choseData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateDialog dialog = new DateDialog(context);
                dialog.setListener(new DateDialog.OnDateChoseListener() {
                    @Override
                    public void onDateClicked(int year, int month, int day) {
                        pointTime.setText(year + "/" +month + "/" +day);
                    }
                });
                dialog.show();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    try {
                        listener.onOkClicked(department.getText().toString(),Integer.parseInt(workTime.getText().toString()),pointTime.getText().toString());
                    }catch (Exception e){
                        Toast.makeText(context,workTime.getText().toString()+"==> ???\n"+e.toString(),Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }
    public interface OnButtonClickListener{
        void onOkClicked(String department,int workTime,String date);
    }
}
